﻿using B.S.Traceability.ErrorCode;
using B.S.Traceability.WebAPI.Write.Commands;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace B.S.Traceability.WebAPI.Write.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ManagementController : ControllerBase
    {
        private readonly IMediator _mediator;

        public ManagementController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 添加商品表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> AddShops(ShopsCommand request)
        {
            return await _mediator.Send(request);
        }

        /// <summary>
        /// 图片上传
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UploadImages(IFormFile file)
        {
            var res = "wwwroot/Images/" + file.FileName;
            using (FileStream fs=new FileStream(res,FileMode.OpenOrCreate))
            {
                file.CopyTo(fs);
            }
            return Ok("http://localhost:17431/Images/" + file.FileName);
        }

        /// <summary>
        /// 批量删除商品表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<int>> DelShops(DelShopsCommand request)
        {
            return await _mediator.Send(request);
        }
    }
}
